<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Change;
use App\Admin\Actions\Sea;
use App\Admin\Actions\Sh;
use App\Exports\ClientExport;
use App\Models\Client;
use App\Models\CrmConfig;
use App\Models\Department;
use App\Models\MoneyOut;
use App\Models\User;
use Carbon\Carbon;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class MoneyOutCheckController extends BaseController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '客户成交审核';

    protected $sh_status = [0=>'待审核',1=>'通过',2=>'驳回',3=>'审核中'];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Client());

        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->disableColumnSelector();
        $grid->exporter(new ClientExport());
        $users = User::Users();
        $grid->model()
            ->where('status',1)->orderBy('created_at','desc');

        $is_leader = true;
        if(Admin::user()->is_leader ==1){
            $ids = User::query()->where('department_id',Admin::user()->department_id)->pluck('id')->toArray();
            $grid->model()->whereIn('employee_id',$ids);
            $is_leader=false;
        }
        $grid->model()->orderBy('created_at', 'desc');

        $grid = $this->permissions($grid, false, false, false,$is_leader,false);

        $grid->column('id',__('ID'))->sortable();
        $grid->column('created_at','签约日期')->display(function ($model){return date('Ymd',$model);});
        $grid->column('name','姓名')->display(function ($model){
            return "<a href='/admin/clients/$this->id/edit'>$model</a>";
        });
        $grid->column('employee_id','所属销售员')->using($users);
        $grid->column('leader','上级主管')->display(function ($model){
            return Department::getLeader($this->employee_id);
        });

        $grid->column('employee.dep_l3','所属三级部门')->using(Department::getLevel(3));
        $grid->column('employee.dep_l2','所属二级部门')->using(Department::getLevel(2));
        $grid->column('employee.dep_l1','所属一级部门')->using(Department::getLevel(1));

        $grid->column('out_id','产品');

        $grid->column('employee_status','销售跟进状态')->using(CrmConfig::getKeyValue('follow_status'));
        $grid->column('quanzheng_status','权证跟进状态')->using(CrmConfig::getKeyValue('quanzheng_status'));
        $grid->column('oneLastRecords.records','最新跟进记录');
        $grid->column('status','客户类型')->using(CrmConfig::getKeyValue('client_status'));

        $grid->column('last_updated_at','最后跟进时间')->display(function ($model){
            return ! empty($model) ? Carbon::parse($model)->diffForHumans() : '';
        });
        self::newestRecords($grid);
        $grid->column('foo','未跟进天数')->display(function ($model){
            $nt = (int) ceil((time() - strtotime($this->last_updated_at)) / 60 / 60 / 24);
            return $nt > 3650 ? '从未' : $nt;
        });

        $grid->column('quanzheng_id','权证人')->using($users);

        $grid->actions(function (Grid\Displayers\Actions $actions){
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();
        });

        $grid->filter(function ($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1/2,function ($filter){
                $filter->equal('employee_status','跟进状态')->select(CrmConfig::getKeyValue('follow_status'));
                $filter->equal('mobile','手机号码');
                $filter->equal('real_finished','审核状态')->select($this->sh_status);
            });
            $filter->column(1/2,function ($filter){
                $filter->like('name','客户姓名');
                $filter->equal('employee_id','所属销售员')->select(User::Users());
                $filter->between('created_at','创建时间')->datetime();
            });
        });

        $grid->batchActions(function ($batch){
            $batch->add(new Sh);
            $batch->disableDelete();
        });



        // 每页条数
        $grid->perPages([10,20,50,100,200,500,1000]);

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(MoneyOut::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Client);

        $form->display('id', __('ID'));
        $form->select('real_finished','审核状态')->options($this->sh_status);
        $form->text('true_client','true_client');
        $form->text('status','status');
        $form->display('created_at', __('Created At'));
        $form->display('updated_at', __('Updated At'));

        $form->saving(function ($form){
            if($form->real_finished == 1){ // 审核通过， true_client = 1;
                $form->true_client = 1;  //转为真实客户
            }else{
                $form->true_client = 0;
            }
        });
        return $form;
    }
}
